package com.yyfax.ph.home.recommendloan;

import android.text.TextUtils;
import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.infrastructure.statistics.StatService;
import com.yyfax.ph.StatIDSet;
import com.yyfax.ph.base.YJPH;
import com.yyfax.ph.products.ProductDetailActivity;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 获取推荐贷款的权限
 */
public class RecommendPermissionPresenter extends Presenter<RecommendPermissionModel, RecommendPermissionPresenter.View> {
	private static final String TAG = RecommendPermissionPresenter.class.getSimpleName();

	public static final int FUNCTION_CODE_RECOMMEND_LAS = 1001;
	public static final int FUNCTION_CODE_RECOMMEND_LOAN = 1002;

	private static final String RECOMMEND_PERMISSION_CODE_NO_PERMISSION = "10001"; // 推荐无权限
	private static final String RECOMMEND_PERMISSION_CODE_NO_BIND_COMPANY = "10002"; // 推荐无权限未绑定公司
	private static final String RECOMMEND_PERMISSION_CODE_APPLYING = "10003"; // 推荐无权限申请中
	private static final String RECOMMEND_PERMISSION_CODE_CHANNEL_EXPIRED = "10009"; // 渠道过期
	private static final String RECOMMEND_PERMISSION_CODE_IS_PARENT_ACCOUNT = "10012"; // 当前用户类型是母账号
	private static final String RECOMMEND_PERMISSION_CODE_NO_REAL_NAME = "10013"; // 未实名制
	private static final String RECOMMEND_PERMISSION_CODE_RECOMMEND_LAS_NOT_OPEN = "10022"; // 云贷推荐暂未上线,敬请期待


	private String mRequestPage;

	public static class Generator implements PresenterFactory<RecommendPermissionPresenter> {
		@Override
		public RecommendPermissionPresenter create() {
			return new RecommendPermissionPresenter();
		}
	}

	public interface View extends IView {
		/**
		 * 账号（任意类型）被禁用
		 */
		void getRcmPermissionNoPermission();

		/**
		 * 用户类型为母账户
		 */
		void getRcmPermissionIsParentAccount();

		/**
		 * 注册时填写公司编号，被母账号拒绝
		 */
		void getRcmPermissionNoBindCompany();

		/**
		 * 注册时填写公司编号，母账号未处理申请
		 */
		void getRcmPermissionApplying();

		/**
		 * 公司编号过期
		 */
		void getRcmPermissionChannelExpired(String company, String phone);

		/**
		 * 用户是否实名认证
		 */
		void getRcmPermissionNoRealName();

		/**
		 * 有权限
		 */
		void getRcmPermissionSuccess();

		/**
		 * 获取推荐贷款的权限失败
		 */
		void getRecommendPassFailure(String tips);

		/**
		 * 获取推荐云贷权限成功
		 * @param shareBean
		 */
		void getLASShareUrlSuccess(RecommendPermissionModel.ShareBean shareBean);

		/**
		 * 云贷推荐暂未开通,敬请期待.
		 */
		void lasShareNotOpen();
		void getLasShareUrlFailed(String tips);
	}

	private View mView;

	@Override
	public void attach(View view) {
		mRequestPage = view.getClass().getName();
		mView = view;
		mModel = new RecommendPermissionModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	/**
	 * 获取云贷推荐分享页面
	 */
	public void getLASShareUrl() {
		execute(null, FUNCTION_CODE_RECOMMEND_LAS, true, null, mRequestPage);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void getLASShareResult(RecommendPermissionModel.LASShareResult shareResult) {
		/*ShareBean shareBean = new ShareBean();
		shareBean.title = "推荐友金云贷，最高可贷50万";
		shareBean.content = "用友集团旗下借款平台，纯线上提交，最高可贷50万，祝您资金周转更轻松！";
		shareBean.url = "http://www.baidu.com";
		mView.getLASShareUrlSuccess(shareBean);*/
		if (mRequestPage.equals(shareResult.requestPage)) {
			if (isResponseSuccess(shareResult)) {
				mView.getLASShareUrlSuccess(shareResult.getContent());
			} else if (isResponseWarning(shareResult)) {
				switch (shareResult.getContent().code) {
					case RECOMMEND_PERMISSION_CODE_NO_PERMISSION:
						mView.getRcmPermissionNoPermission();
						break;
					case RECOMMEND_PERMISSION_CODE_NO_BIND_COMPANY:
						mView.getRcmPermissionNoBindCompany();
						break;
					case RECOMMEND_PERMISSION_CODE_APPLYING:
						mView.getRcmPermissionApplying();
						break;
					case RECOMMEND_PERMISSION_CODE_CHANNEL_EXPIRED:
						mView.getRcmPermissionChannelExpired(shareResult.getContent().companyName, shareResult.getContent().companyPhone);
						break;
					case RECOMMEND_PERMISSION_CODE_IS_PARENT_ACCOUNT:
						mView.getRcmPermissionIsParentAccount();
						break;
					case RECOMMEND_PERMISSION_CODE_NO_REAL_NAME:
						mView.getRcmPermissionNoRealName();
						break;
					case RECOMMEND_PERMISSION_CODE_RECOMMEND_LAS_NOT_OPEN:
						mView.lasShareNotOpen();
						break;
					default:
						mView.getLasShareUrlFailed(TextUtils.isEmpty(shareResult.getMsg()) ? "获取分享链接失败" : shareResult.getMsg());
						break;
				}
			} else {
				mView.getLasShareUrlFailed("获取分享链接失败");
			}
		}
	}

	/**
	 * 获取推荐贷款权限
	 */
	public void getRcmPermission() {
		execute(null, FUNCTION_CODE_RECOMMEND_LOAN, true, null, mRequestPage);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void allowedResult(RecommendPermissionModel.PermissionResult result) {
		CLog.i(TAG, "rs=" + result.toString());
		if(TextUtils.equals(mRequestPage, ProductDetailActivity.class.getName())){
			StatService.onEventFirstKeyAttribute(YJPH.getInstance(), StatIDSet.EVENT_ID_C005, result.getStatus());
		}else {
			StatService.onEventFirstKeyAttribute(YJPH.getInstance(), StatIDSet.EVENT_ID_B001, result.getStatus());
		}
		if (mRequestPage.equals(result.requestPage)) {
			if (isResponseSuccess(result)) {
				mView.getRcmPermissionSuccess();
			} else if (isResponseWarning(result) && null != result.getContent() && !TextUtils.isEmpty(result.getContent().code)) {
				switch (result.getContent().code) {
					case RECOMMEND_PERMISSION_CODE_NO_PERMISSION:
						mView.getRcmPermissionNoPermission();
						break;
					case RECOMMEND_PERMISSION_CODE_NO_BIND_COMPANY:
						mView.getRcmPermissionNoBindCompany();
						break;
					case RECOMMEND_PERMISSION_CODE_APPLYING:
						mView.getRcmPermissionApplying();
						break;
					case RECOMMEND_PERMISSION_CODE_CHANNEL_EXPIRED:
						mView.getRcmPermissionChannelExpired(result.getContent().companyName, result.getContent().companyPhone);
						break;
					case RECOMMEND_PERMISSION_CODE_IS_PARENT_ACCOUNT:
						mView.getRcmPermissionIsParentAccount();
						break;
					case RECOMMEND_PERMISSION_CODE_NO_REAL_NAME:
						mView.getRcmPermissionNoRealName();
						break;
					default:
						mView.getRecommendPassFailure(TextUtils.isEmpty(result.getMsg()) ? "检查权限失败" : result.getMsg());
						break;
				}

			} else {
				mView.getRecommendPassFailure(TextUtils.isEmpty(result.getMsg()) ? "检查权限失败" : result.getMsg());
			}
		}
	}
}
